package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunBodyGroupBindMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunBodyGroupBind;
import com.csun.cmny.provider.model.domain.DmcCsunBodySensorLocation;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.vo.csunbody.BodyGroupBindVo;
import com.csun.cmny.provider.model.vo.csunbody.BodyGroupVo;
import com.csun.cmny.provider.service.DmcCsunBodyGroupBindService;
import com.csun.cmny.provider.service.DmcCsunBodySensorLocationService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-09-22 15:54
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcCsunBodyGroupBindServiceImpl extends BaseService<DmcCsunBodyGroupBind> implements DmcCsunBodyGroupBindService {

    @Resource
    private DmcCsunBodyGroupBindMapper dmcCsunBodyGroupBindMapper;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcCsunBodySensorLocationService dmcCsunBodySensorLocationService;

    /**
     * query list by id
     *
     * @param id the id
     *
     * @return the list.
     */
    @Override
    public List<BodyGroupBindVo> queryListById(Long id) {

        return dmcCsunBodyGroupBindMapper.queryListById(id);
    }

    /**
     * bind
     *
     * @param dmcCsunBodyGroupBind the dmc csun body group bind
     * @param loginAuthDto         the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer bind(DmcCsunBodyGroupBind dmcCsunBodyGroupBind, LoginAuthDto loginAuthDto) {

        DmcDevice dmcDevice = dmcDeviceService.selectByKey(dmcCsunBodyGroupBind.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100619003);
        }

        DmcCsunBodySensorLocation dmcCsunBodySensorLocation = dmcCsunBodySensorLocationService
                .selectByKey(dmcCsunBodyGroupBind.getDeviceId());
        if (PublicUtil.isEmpty(dmcCsunBodySensorLocation)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100619004);
        }

        DmcCsunBodyGroupBind record = new DmcCsunBodyGroupBind();
        record.setDeviceId(dmcCsunBodyGroupBind.getDeviceId());
        int count = dmcCsunBodyGroupBindMapper.selectCount(record);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100619005);
        }

        return dmcCsunBodyGroupBindMapper.insertSelective(dmcCsunBodyGroupBind);
    }

    /**
     * untied
     *
     * @param dmcCsunBodyGroupBind the dmc csun body group bind
     * @return the int.
     */
    @Override
    public Integer untied(DmcCsunBodyGroupBind dmcCsunBodyGroupBind) {

        return dmcCsunBodyGroupBindMapper.delete(dmcCsunBodyGroupBind);
    }

    /**
     * the query body group by device no
     *
     * @param bodySensorId the body sensor id
     *
     * @return the body group vo
     */
    @Override
    public BodyGroupVo queryBodyGroupByDeviceNo(String bodySensorId) {

        return dmcCsunBodyGroupBindMapper.queryBodyGroupByDeviceNo(bodySensorId);
    }
}
